#pragma semicolon 1
#include <sourcemod>
#include <sdktools>

#include <l4d_stocks>
#include "macros.sp"
#include "helpers.sp"

#define PLUGIN_VERSION "1.2"

new Handle:cvar_slapPower = INVALID_HANDLE;
new Handle:cvar_slapCooldownTime = INVALID_HANDLE;
new Handle:cvar_multpower = INVALID_HANDLE;

new bool:can_slap[MAXCLIENTS] = { true, ... };

public Plugin:myinfo = 
{
	name = "L4D2 Boomer Bitch Slap",
	author = " AtomicStryker",
	description = "Left 4 Dead 2 Boomer Bitch Slap",
	version = PLUGIN_VERSION,
	url = "http://forums.alliedmods.net/showthread.php?t=97952"
}

public OnPluginStart()
{
	Require_L4D2();

	CreateConVar("l4d2_boomerbitchslap_version", PLUGIN_VERSION, " L4D2 Boomer Bitch Slap Plugin Version ", CVAR_FLAGS|FCVAR_DONTRECORD);
	
	cvar_slapPower = CreateConVar("l4d2_boomerbitchslap_power", "300.0", " How much Force is applied to the victim ", CVAR_FLAGS);
	cvar_slapCooldownTime = CreateConVar("l4d2_boomerbitchslap_cooldown", "15.0", " How many seconds before Boomer can Slap again ", CVAR_FLAGS);
	cvar_multpower = CreateConVar("l4d2_boomerbitchslap_mult","1.5", "multiplicator to x and y axis", CVAR_FLAGS);

	AutoExecConfig(true, "l4d2_boomerbitchslap");

	HookEvent("player_hurt", PlayerHurt);
}

public Action:PlayerHurt(Handle:event, const String:name[], bool:dontBroadcast)
{
	new slapper = GetClientOfUserId(GetEventInt(event, "attacker"));
	new target = GetClientOfUserId(GetEventInt(event, "userid"));
	
	if(IsValidInfectedAlive(slapper) && IsValidSurvivorAlive(target))
	{
		decl String:weapon[32];
		GetEventString(event, "weapon", weapon, sizeof(weapon));
		if(strcmp(weapon, "boomer_claw") == 0)
		{
			if(can_slap[slapper])
			{
				can_slap[slapper] = false;

				decl Float:aim[3];
				GetAimVector(slapper, aim);
				
				decl Float:vel[3];
				GetEntPropVector(target, Prop_Data, "m_vecVelocity", vel);
				
				decl Float:boomer_vel[3];
				GetEntPropVector(slapper, Prop_Data, "m_vecVelocity", boomer_vel);
				new Float:length = GetVectorLength(boomer_vel);
				
				
				decl Float:resulting[3];
				resulting[0] = FloatAdd(vel[0], aim[0]);
				resulting[1] = FloatAdd(vel[1], aim[1]);
				resulting[0] += (length * GetConVarFloat(cvar_multpower));
				resulting[1] += (length * GetConVarFloat(cvar_multpower));
				resulting[2] = GetConVarFloat(cvar_slapPower);

				TeleportEntity(target, NULL_VECTOR, NULL_VECTOR, resulting);
				
				PrintToChatAllExclude(target, slapper, "\x04%N\x01 was \x02Bitch Slapped\x01 by \x04%N\x01!", target, slapper);
				PrintToChatOnly(target, slapper, "\x05%N\x01 was \x02Bitch Slapped\x01 by \x05%N\x01!", target, slapper);

				CreateTimer(GetConVarFloat(cvar_slapCooldownTime), can_slap_again, slapper, TIMER_FLAG_NO_MAPCHANGE);
			}
		}
	}	
}

public OnMapStart()
{
	for(new i=0; i < MAXCLIENTS; i++)
	{
		can_slap[i] = true;
	}
}

public Action:can_slap_again(Handle:timer, any:client)
{
	if(IsValidClient(client))
	{
		can_slap[client] = true;
	}
}